<template>
    <el-main>
        <el-row :gutter="20">
            <el-col :span="7" class="mian-aside">
                <!-- 当前问题所属用户 -->
                <userInfo :keys="keys" @getQuestionCommentByQuestionID="getQuestionCommentByQuestionID"></userInfo>
                <!-- VIP广告-->
                <advSystem></advSystem>
            </el-col>
            <el-col :span="17">
                <el-dialog title="匿名举报，为防止滥用，仅管理员可见举报者" :visible.sync="dialogReportVisible" :show-close="false" :close-on-click-modal="false"
                    @close="dialogReportClose">
                    <el-form :model="reportForm" :rules="rules" ref="reportForm">
                        <el-divider content-position="left">我要举报该问题，理由是：</el-divider>
                        <el-input type="hidden" v-model="reportForm.answer_id" autocomplete="off"></el-input>
                        <el-form-item label="举报类型" prop="report_type"><br/>
                            <el-checkbox-group v-model="reportForm.report_type">
                                <el-checkbox label="垃圾广告:恶意灌水、广告、推广等内容" name="report_type"></el-checkbox><br/>
                                <el-checkbox label="无意义内容:测试、灌水、文不对题、消极内容、问题品质太差等" name="report_type"></el-checkbox><br/>
                                <el-checkbox label="违规内容:色情、暴力、血腥、敏感信息等" name="report_type"></el-checkbox><br/>
                                <el-checkbox label="不友善内容:人身攻击、挑衅辱骂、恶意行为" name="report_type"></el-checkbox><br/>
                                <el-checkbox label="标题随意:标题党、标题不释义" name="report_type"></el-checkbox><br/>
                                <el-checkbox label="错误分类:问题话题选择不合适" name="report_type"></el-checkbox><br/>
                                <el-checkbox label="严重非法言论:侮辱国家、发布谣言、邪教信仰、传销性质等" name="report_type"></el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                        <el-form-item label="举报描述" prop="report_descrption">
                            <el-input type="textarea" v-model="reportForm.report_descrption"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogReportVisible = false">取 消</el-button>
                        <el-button type="primary" @click="reportSubmit('reportForm')">提 交</el-button>
                    </div>
                </el-dialog>
                <el-card class="box-card" style="min-height: 13.75rem;">
                    <avue-article :option="QSoption" :data="QSdata"></avue-article>
                </el-card>
                <el-card class="box-card" style="min-height: 13.75rem;">
                    <el-row v-if="QSdata.close_comment === 1" style="height: 350px;">
                        <div>
                            <el-divider content-position="left">请勿发布不友善或者负能量的内容。与人为善，比聪明更重要！</el-divider>
                            <Editor v-model="content"></Editor>
                            <el-button style="float: right;margin-top: 75px;" type="primary" icon="el-icon-chat-line-round" @click="handlerComment">评论</el-button>
                        </div>
                    </el-row>
                    <el-row>
                        <div v-for="(item, key) in CMdata" :key="item.id">
                            <avue-comment :option="CMoption" :data="item" :reverse="item.authBtns.reverse">
                                <el-row>
                                    <el-button icon="el-icon-delete" type="danger" title="删除" size="mini" circle v-if="item.authBtns.delBtn === 1"
                                        @click="handlerCommenrDel(item.id, key)"></el-button>

                                    <el-button icon="el-icon-star-off" type="warning" title="点赞" size="mini" circle
                                        v-if="item.authBtns.voteBtn === 1 && item.hasVote === 0" @click="handlerVote(item.id)"></el-button>
                                    <el-button icon="el-icon-star-on" type="success" title="取消点赞" size="mini" circle
                                        v-if="item.authBtns.voteBtn === 1 && item.hasVote === 1" @click="handlerCancelVote(item.id)"></el-button>

                                    <el-button icon="el-icon-circle-check" type="info" title="采纳" size="mini" circle
                                        v-if="QSdata.cates !== 'blog' && item.authBtns.adoptBtn === 1 && item.hasAdopt === 0" @click="handlerAdopt(item)"></el-button>
                                    <el-button icon="el-icon-success" type="success" title="已采纳" size="mini" circle
                                        v-if="QSdata.cates !== 'blog' && item.authBtns.adoptBtn === 1 && item.hasAdopt === 1"></el-button>

                                    <el-button icon="el-icon-s-flag" type="danger" title="举报" size="mini" circle v-if="item.authBtns.reportBtn === 1"
                                        @click="handlerAnswerReport(item.id)"></el-button>

                                    <el-button icon="el-icon-user" type="info" title="关注用户" size="mini" circle v-if="item.authBtns.followeruserBtn === 1 && item.hasFollower === 0"
                                        @click="handlerFollowerUser(item.user_id)"></el-button>
                                    <el-button icon="el-icon-user-solid" type="success" title="取消关注" size="mini" circle v-if="item.authBtns.followeruserBtn === 1 && item.hasFollower === 1"
                                        @click="handlerCancelFollowerUser(item.user_id)"></el-button>
                                </el-row>
                            </avue-comment>
                        </div>
                    </el-row>
                </el-card>
            </el-col>
        </el-row>
    </el-main>
</template>

<script>
    import Editor from '../../components/Editor'
    import advSystem from '../../components/advSystem'
    import userInfo from '../../components/userInfo'
    export default {
        inject: ['reload'],
        data() {
            return {
                keys: 'questionComment',
                dialogReportVisible: false,
                formLabelWidth: '120px',
                reportForm: {
                    answer_id: 0,
                    report_type: [],
                    report_descrption: ''
                },
                rules: {
                    report_type: [{
                        type: 'array',
                        required: true,
                        message: '请至少选择一个类型',
                        trigger: 'change'
                    }],
                    report_descrption: [{
                        required: true,
                        message: '请填写举报描述',
                        trigger: 'blur'
                    }, { min: 10, max: 200, message: '长度在 10 到 200 个字符', trigger: 'blur' }]
                },
                content: '',
                question_id: 0,
                QSdata: {},
                QSoption: {
                    props: {
                        title: 'title',
                        meta: 'meta',
                        lead: 'lead',
                        body: 'body'
                    },
                },
                CMdata: {},
                CMoption: {
                    props: {
                        avatar: 'avatar',
                        author: 'author',
                        body: 'body'
                    }
                }
            }
        },
        components: {
            Editor,
            advSystem,
            userInfo
        },
        created() {
            this.getQuestionCommentByQuestionID()
        },
        methods: {
            // 评论
            handlerComment () {
                const params = {
                    question_id: this.$route.query.question_id,
                    content: this.content
                }

                this.handleAxiosFun(1, this.$API.ZHIHUAPI.answerQuestion, params)
                this.getQuestionCommentByQuestionID()
                this.content = ''
            },
            // 评论删除
            handlerCommenrDel (id, i) {
                const params = {
                    id: id
                }
                this.handleAxiosFun(0, this.$API.ZHIHUAPI.removeAnswerQuestion, params)
                this.CMdata.splice(i, 1)
            },
            // 关注用户
            handlerFollowerUser (userId) {
                const params = {
                    user_id: userId,
                }
                // console.log(userId)
                this.handleAxiosFun(0, this.$API.ZHIHUAPI.followerUser, params)
                this.getQuestionCommentByQuestionID()
            },
            // 取消关注用户
            handlerCancelFollowerUser (userId) {
                const params = {
                    user_id: userId,
                }
                this.handleAxiosFun(0, this.$API.ZHIHUAPI.followerCancelUser, params)
                this.getQuestionCommentByQuestionID()
            },
            // 点赞
            handlerVote (answerId) {
                const params = {
                    answer_id: answerId,
                }
                this.handleAxiosFun(0, this.$API.ZHIHUAPI.answerQuestionVote, params)
                this.getQuestionCommentByQuestionID()
            },
            // 取消点赞
            handlerCancelVote (answerId) {
                const params = {
                    answer_id: answerId,
                }
                this.handleAxiosFun(0, this.$API.ZHIHUAPI.answerQuestionCancelVote, params)
                this.getQuestionCommentByQuestionID()
            },
            // 评论采纳
            handlerAdopt (item) {
                const params = {
                    answer_id: item.id,
                }
                var _this = this
                _this.$confirm('是否确认采纳该答案,一旦采纳将不可取消！', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function() {
                    _this.handleAxiosFun(0, _this.$API.ZHIHUAPI.answerQuestionAdopt, params)
                    _this.getQuestionCommentByQuestionID()
                }).catch(() => {})
            },
            // 举报
            handlerAnswerReport (id) {
                this.dialogReportVisible = true
                this.reportForm.answer_id = id
            },
            reportSubmit(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.handleAxiosFun(0, this.$API.ZHIHUAPI.reportAnswer, this.reportForm)
                        this.dialogReportVisible = false
                    } else {
                        return false;
                    }
                });
            },
            dialogReportClose() {
                this.$refs.reportForm.resetFields();
            },
            // 获取信息
            getQuestionCommentByQuestionID() {
                const _this = this
                const params = {
                    question_id: _this.$route.query.question_id
                }
                _this.axios.post(_this.$API.ZHIHUAPI.getQuestionCommentByQuestionID, _this.qs.stringify(params)).then((
                    res) => {
                    res = res.data
                    // console.log(res)
                    if (res.code === 10000) {
                        if (res.hasOwnProperty('data') && Object.keys(res.data).length > 0) {
                            _this.QSdata = res.data.questionData
                            _this.CMdata = res.data.commentData
                        } else {
                            _this.QSdata = {}
                            _this.CMdata = {}
                        }
                    } else {
                        _this.$message({
                            showClose: false,
                            message: res.message,
                            type: 'error'
                        })
                        _this.QSdata = {}
                        _this.CMdata = {}
                    }
                }).catch(() => {
                    _this.$message({
                        showClose: false,
                        message: '网络连接失败，请重试！',
                        type: 'error'
                    })
                    _this.QSdata = {}
                    _this.CMdata = {}
                })
            },
            handleAxiosFun(tips, url, params, done, loading) {
                const _this = this
                _this.axios.post(url, _this.qs.stringify(params)).then((res) => {
                    if (res.data.code === 10000) {
                        if (tips === 0) {
                            _this.$message({
                                showClose: false,
                                message: res.data.message,
                                type: 'success'
                            })
                        }
                        // _this.refresh()
                        if (done) {
                            done()
                        }
                    } else {
                        _this.$message({
                            showClose: false,
                            message: res.data.message,
                            type: 'error'
                        })
                        if (loading) {
                            loading()
                        }
                    }
                }).catch(() => {
                    if (tips === 0) {
                        _this.$message({
                            showClose: false,
                            message: '网络连接失败，请重试！',
                            type: 'error'
                        })
                    }
                    if (loading) {
                        loading()
                    }
                })
            }
        }
    }
</script>

<style scoped>
</style>
